package com.guangaiboen.attendance.core.dispatcher.executor;


import com.guangaiboen.attendance.core.dispatcher.constants.RequestTypes;
import com.guangaiboen.attendance.core.dispatcher.context.DispatcherContext;
import com.guangaiboen.attendance.core.dispatcher.utils.XMLUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

import java.nio.charset.Charset;

/**
 * Xml请求
 * @param <T>
 */
public abstract  class XMLHttpDispatcherExecutor<T> extends PostHttpDispatcherExecutor<T> {

    private static final String CONTENT_TYPE = "application/xml";

    @Override
    protected StringEntity createEntity(String url, DispatcherContext dispatcherContext) {
        Object dispatcherParameters = getDispatcherParameters(url,dispatcherContext);
        String xmlBody = XMLUtils.toXMLString(dispatcherParameters);
        return new StringEntity(xmlBody,getContentType());
    }

    protected abstract Object getDispatcherParameters(String url, DispatcherContext dispatcherContext);

    protected Charset getCharset(){
        return DEFAULT_CHARSET;
    }

    protected ContentType getContentType(){
        return ContentType.create(CONTENT_TYPE,getCharset());
    }

    @Override
    protected String getRequestTypes() {
        return RequestTypes.XML.getRequestCode();
    }

}
